package com.hjwoo.app.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjwoo.app.been.OrdersObject;

import java.lang.reflect.Type;
import java.util.LinkedList;

/**
 * Created by yangchuan on 2016/5/9.
 */
public class JsonParser
{
    private static JsonParser jsonParser = null;

    private JsonParser()
    {
    }

    public static JsonParser getJsonParser()
    {
        if (jsonParser == null)
        {
            synchronized (JsonParser.class)
            {
                if (jsonParser == null)
                {
                    jsonParser = new JsonParser();
                }
            }
        }
        return jsonParser;
    }

    public synchronized <T> T parserJsonToObj(String jsonString, Class<T> tClass)
    {
        T t = null;
        try
        {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, tClass);
        }
        catch (Exception e)
        {
            Log.e("JsonParser异常——》", e.toString());
        }
        return t;
    }

//    public synchronized <T> List<T> parserJosnToList(String jsonString, Class<T> cls) {
//        Gson gson = new Gson();
//        List<T> list = gson.fromJson(jsonString, new TypeToken<List<T>>() {
//        }.getType());
//        return list;
//    }

    /**
     * 返回list对象
     *
     * @param jsonString
     * @return
     */

    public LinkedList<OrdersObject> parserJosnToList(String jsonString)
    {
        LinkedList<OrdersObject> orders = null;
//        Gson gson = new Gson();
//        List<T> list = gson.fromJson(jsonString, new TypeToken<List<T>>() {
//        }.getType());
        try
        {

            Type listType = new TypeToken<LinkedList<OrdersObject>>()
            {
            }.getType();
            Gson gson = new Gson();
            orders = gson.fromJson(jsonString, listType);
        }
        catch (Exception e)
        {
            Log.e("JsonParser异常——》", e.toString());
        }
        return orders;

    }

}
